package com.scqkxx.gamebox.activity.news;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.MainActivity;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.news.adapter.NewsAdapter;
import com.scqkxx.gamebox.databinding.ActivityTrendsBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.widget.CardSwipeLayout.CardItemTouchHelperCallback;
import com.scqkzqtz.base.widget.CardSwipeLayout.CardLayoutManager;
import com.scqkzqtz.base.widget.CardSwipeLayout.OnSwipeListener;

import java.util.ArrayList;
import java.util.List;

public class NewsActivity extends BaseActivity<ActivityTrendsBinding> {

    private List<AVObject> resourceList = new ArrayList<>();//原始数据
    private List<AVObject> dataList = new ArrayList<>();
    private NewsAdapter trendsAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_trends;
    }

    @Override
    public void init() {
        binding.mTitleView.getTv_lift().setVisibility(View.INVISIBLE);
        binding.mTitleView.setOnTitleClickListener((int titleType) -> finish());
        binding.btSkip.setOnClickListener(view -> {
            ActivityUtil.switchTo(NewsActivity.this , MainActivity.class);
            finish();
        });

        trendsAdapter = new NewsAdapter(R.layout.item_trends,dataList);
        binding.rvTrends.setAdapter(trendsAdapter);
        getData();

        CardItemTouchHelperCallback cardCallback = new CardItemTouchHelperCallback(binding.rvTrends.getAdapter(), dataList);
        ItemTouchHelper touchHelper = new ItemTouchHelper(cardCallback);
        CardLayoutManager cardLayoutManager = new CardLayoutManager(binding.rvTrends, touchHelper);
        binding.rvTrends.setLayoutManager(cardLayoutManager);
        touchHelper.attachToRecyclerView(binding.rvTrends);

        trendsAdapter.setOnSwipItemListener(new NewsAdapter.OnSwipItemListener() {
            @Override
            public void onSwipe(RecyclerView.ViewHolder viewHolder) {
                touchHelper.startSwipe(viewHolder);
            }
        });

        cardCallback.setOnSwipedListener(new OnSwipeListener<AVObject>() {

            @Override
            public void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {

            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, AVObject t, int direction) {

            }

            @Override
            public void onSwipedClear() {
                getData();
            }

        });

    }

    private void getData(){
        AVQuery<AVObject> query = AVQuery.getQuery("News");
        query.whereEqualTo("belongGroupId",AVUser.getCurrentUser().getInt("defaultGroupId"));
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("avObjects News",avObjects+"");
                if(avException == null){
                    dataList.clear();
                    resourceList.clear();
                    dataList.addAll(avObjects);
                    resourceList.addAll(avObjects);
                    trendsAdapter.notifyDataSetChanged();
                }
            }
        });
    }

}
